<template>
    <el-row class="attention-box">
        <el-col :span="20" :offset="2">
            <el-row :gutter="20" v-if="userInfos&&userInfos.length>0">
                <el-col :span="4" v-for="userInfo in userInfos" :key="userInfo.id">
                    <simple-user-info :user="userInfo"></simple-user-info>
                </el-col>
            </el-row>
            <div class="none" v-else>
                没有任何关注信息
            </div>
            <el-row>
                <el-col :span="18" :offset="3">
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageInfo.page"
                        :page-sizes="pageInfo.sizes"
                        :page-size="pageInfo.size"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageInfo.total">
                    </el-pagination>
                </el-col>
            </el-row>
        </el-col>
    </el-row>
</template>

<script>
    import { mapActions } from 'vuex'
    import SimpleUserInfo from '@/components/user/SimpleUserInfo'

    export default {
        name: 'attention',
        components: {
            SimpleUserInfo
        },
        data () {
            return {
                pageInfo: {
                    page: 1,
                    size: 20,
                    total: 0,
                    sizes: [20, 50, 100, 200, 300, 400]
                },
                userInfos: []
            }
        },
        mounted () {
            this.obtainUsers()
        },
        updated () {
            this.obtainUsers()
        },
        methods: {
            ...mapActions({
                pageAttentionAreaUsersAction: 'area/pageAttentionAreaUsers',
                pageAttentionLabelUsersAction: 'label/pageAttentionLabelUsers'
            }),
            getQueryInfo () {
                let paths = this.$route.path.split('/')
                return { id: paths.pop(), type: paths.pop() }
            },
            handleSizeChange (val) {
                this.pageInfo.size = val
                this.obtainUsers()
            },
            handleCurrentChange (val) {
                this.pageInfo.page = val
                this.obtainUsers()
            },
            obtainUsers () {
                let queryInfo = this.getQueryInfo()
                switch (queryInfo.type) {
                    case 'area':
                        this.obtainUserInfos(this.pageAttentionAreaUsersAction({
                            areaId: queryInfo.id,
                            page: this.pageInfo.page,
                            size: this.pageInfo.size
                        }))
                        break
                    case 'label':
                        this.obtainUserInfos(this.pageAttentionLabelUsersAction({
                            labelId: queryInfo.id,
                            page: this.pageInfo.page,
                            size: this.pageInfo.size
                        }))
                        break
                }
            },
            obtainUserInfos (action) {
                this.action(action, data => {
                    if (data.data) {
                        this.pageInfo.total = data.data.total
                        this.userInfos = data.data.records
                    }
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .attention-box {
        margin: 32px 0;

        .none {
            height: 300px;
            line-height: 300px;
        }
    }
</style>
